<template>
  <div class="course-selection-container">
    <!-- 添加 table-layout="auto" 和 max-height 实现自适应 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      :fit="true"
      table-layout="auto"
      @selection-change="handleSelectionChange"
      header-row-class-name="custom-header-row"
    >
      <!-- 调整列宽为自适应 -->
      <el-table-column 
        type="selection" 
        width="60"
        align="center"
        header-align="center"
        class-name="selection-column"
      >
        <template slot="header">
          <div class="header-checkbox-wrapper">
            <el-checkbox 
              :indeterminate="isIndeterminate" 
              v-model="checkAll"
              @change="handleCheckAllChange"
            ></el-checkbox>
          </div>
        </template>
      </el-table-column>

      <!-- 将固定宽度改为 min-width -->
      <el-table-column 
        prop="courseName" 
        label="课程名称" 
        min-width="180"
      />
      <el-table-column 
        prop="university" 
        label="所属大学" 
        min-width="220"
      />
      <el-table-column 
        prop="credit" 
        label="学分" 
        width="90"
        align="center"
      />
      <el-table-column 
        prop="semester" 
        label="学期" 
        width="120"
        align="center"
      />
      <el-table-column 
        prop="teacher" 
        label="授课教师" 
        min-width="150"
      />
    </el-table>

    <!-- 添加按钮容器样式 -->
    <div class="action-buttons">
      <el-button type="primary" @click="submitSelection">提交所选课程</el-button>
      <el-popover
        placement="top"
        width="220"
        v-model="visible"
        trigger="click">
        <p>确认要取消所有课程选择吗？</p>
        <div class="popconfirm-footer">
          <el-button size="mini" @click="visible = false">取消</el-button>
          <el-button type="primary" size="mini" @click="handleConfirm">确定</el-button>
        </div>
        <el-button slot="reference" type="info">取消选择</el-button>
      </el-popover>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      checkAll: false,
      isIndeterminate: false,
      tableData: [
          {
            courseName: '物联网工程导论',
            university: '山西农大主体体育场工程',
            credit: 3,
            semester: '2026春季',
            teacher: '王建国教授'
          },
          {
            courseName: '智能建筑信息系统',
            university: '太原理工大学一号教学楼工程',
            credit: 4,
            semester: '2026秋季',
            teacher: '李振华副教授'
          },
          {
            courseName: '工程大数据分析',
            university: '中北大学',
            credit: 3,
            semester: '2026秋季',
            teacher: '张伟教授'
          },
          {
            courseName: 'BIM技术应用',
            university: '兰州理工大学',
            credit: 2,
            semester: '2026春季',
            teacher: '陈敏高级工程师'
          },
          {
            courseName: '智能监测系统设计',
            university: '盐城师范学院',
            credit: 3,
            semester: '2026秋季',
            teacher: '周晓琳教授'
          },
          {
            courseName: '工程信息安全管理',
            university: '盐城师范学院',
            credit: 2,
            semester: '2026夏季',
            teacher: '吴刚副教授'
          },
          {
            courseName: '人工智能与工程决策',
            university: '山西农大主体体育场工程',
            credit: 4,
            semester: '2026秋季',
            teacher: '赵志强教授'
          },
          {
            courseName: '工程数据库原理',
            university: '太原理工大学一号教学楼工程',
            credit: 3,
            semester: '2026冬季',
            teacher: '徐芳高级讲师'
          },
          {
            courseName: '智能硬件开发',
            university: '中北大学',
            credit: 3,
            semester: '2026春季',
            teacher: '孙伟副教授'
          },
          {
            courseName: '工程云计算应用',
            university: '兰州理工大学',
            credit: 3,
            semester: '2026秋季',
            teacher: '黄丽华教授'
          },
          {
            courseName: '数字孪生技术',
            university: '盐城师范学院',
            credit: 2,
            semester: '2026夏季',
            teacher: '郑浩工程师'
          },
          {
            courseName: '工程物联网协议',
            university: '盐城师范学院',
            credit: 3,
            semester: '2026春季',
            teacher: '王海涛教授'
          }
        ],
      multipleSelection: []
    }
  },
  methods: {
    handleCheckAllChange(val) {
      this.$refs.multipleTable.toggleAllSelection()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      const total = this.tableData.length
      this.isIndeterminate = val.length > 0 && val.length < total
      this.checkAll = val.length === total
    },
    submitSelection() {
      console.log('提交数据:', this.multipleSelection)
      this.$message.success(`已提交 ${this.multipleSelection.length} 项选择课程`)
    },
    handleConfirm() {
      this.visible = false
      this.$refs.multipleTable.clearSelection()
      this.$message.success('已清除所有选择课程')
    }
  }
}
</script>

<style lang="scss" scoped>
.course-selection-container {
  height: 100%;
  display: flex;
  flex-direction: column;

  ::v-deep .el-table {
    flex: 1;
    overflow: hidden;
    
    &__body-wrapper {
      overflow-x: auto;
    }
  }

  ::v-deep .custom-header-row {
    th.selection-column {
      padding: 12px 0 !important;
    }
  }

  ::v-deep .header-checkbox-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    padding-left: 5px;
    
    .el-checkbox {
      transform: translateY(1px);
    }
  }

  ::v-deep .el-table__body {
    .el-checkbox {
      margin-left: 5px;
    }
  }

  .action-buttons {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;  /* 按钮右对齐 */
    gap: 12px;
    flex-wrap: wrap;  /* 允许按钮换行 */

    /* 小屏幕优化 */
    @media (max-width: 768px) {
      justify-content: center;
      button {
        width: 100%;
        margin-left: 0 !important;
      }
    }
  }

  .popconfirm-footer {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
    gap: 8px;
  }
}
</style>